# -*- coding: utf-8 -*-
'''
Created on Sep 4, 2013

@author: LONG HOANG GIANG
'''
from lxml import etree
import cStringIO
from readability.readability import Document
from BeautifulSoup import BeautifulSoup
from urlparse import urljoin
import re

def extractText(pattern, match, idx, default=''):
    try:
        m = re.search(pattern, match)
        if m != None: 
            return m.group(idx)
    except IndexError:
        pass
    return default

def removeAttribElement(elem, attribName):
    try:
        del elem.attrib[attribName]
    except:
        pass

class HttpResponse():
    
    def __init__(self, html, header):
        self.html = html
        self.header = header
        
    def get_html(self):
        return self.html
    
    def get_cookie(self):
        return self.header.get('Set-Cookie', '')
    
    def get_header(self):
        return self.header
    
    def build_tree(self, base_url=None, attr_width=None):
        headers = self.get_header()
        contentType = headers.get('Content-Type')
        _encoding = extractText(r";charset=(.+)", contentType, 1)
        if _encoding == '': _encoding = 'UTF-8'
        parser = etree.HTMLParser(encoding=_encoding)
        tree = etree.parse(cStringIO.StringIO(self.html), parser, base_url=base_url)
        if base_url != None and base_url != '':
            for aNode in tree.xpath("//a"):
                href = aNode.get('href')
                if href != '':
                    aNode.set('href', urljoin(base_url, href))
            for aINode in tree.xpath("//img"):
                src = aINode.get('src')
                if src != '':
                    aINode.set('src', urljoin(base_url, src))
                if attr_width != None:
                    aINode.set('width', attr_width)
                    removeAttribElement(aINode, 'height')
        return tree
    
    def get_article_content(self):
        readable_article = Document(self.html).summary()
        readable_article = readable_article[17:-20]
        soup = BeautifulSoup(readable_article, convertEntities=BeautifulSoup.HTML_ENTITIES)
        return unicode(soup.body or soup).encode('utf-8')